﻿using System.Drawing;
using System.Windows.Forms;
using GameDesigner.Displayers;
using GameDesigner.Source.Displayers;
using GameDesigner.Source.Elements;
using GameDesigner.Source.Widgets;
using GameLibrary.Model;
using GameLibrary.Model.Core;
using GameLibrary.Model.Play;
using Microsoft.VisualBasic.PowerPacks;
using G.Model.Runtime;
using GameRuntimeLibrary.Model.Runtime;
using G.Runtime;
using System.Collections.Generic;

namespace GameDesigner.Source.Forms
{
    public partial class PlayForm : Form
    {
        private readonly Game _Game;
        private readonly GameDef _GameDef;
        private readonly ShapeContainer _Container = new ShapeContainer();
        private readonly RectangleShape _ScoreBoardShape = new RectangleShape();
        private readonly RectangleShape _InputWidgetShape = new RectangleShape();
        private readonly RectangleShape _MessageShape = new RectangleShape();
        private readonly List<RectangleShape> _PlayerValueShapes = new List<RectangleShape>();

        private GameDisplayerPT _GameDisplayer;
        
        public Game Game
        {
            get { return _Game; }
        }

        public PlayForm()
        {
            InitializeComponent();
        }

        public PlayForm(GameDef pGameDef)
        {
            _GameDef = pGameDef;
            _Game = GameFactory.Factory.CreateGame(_GameDef);
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            this.panel1.Controls.Add(this._Container);

            _Container.Dock = DockStyle.Fill;

            int y = 5;
            const int height = 90;
            {
                _ScoreBoardShape.Location = new Point(505, y);
                _ScoreBoardShape.Size = new Size(150, height);
                _ScoreBoardShape.BackStyle = BackStyle.Opaque;
                _ScoreBoardShape.BackColor = Color.WhiteSmoke;
                _ScoreBoardShape.CornerRadius = 5;

                ScoreBoardPanel painter = new ScoreBoardPanel(_Game, _ScoreBoardShape);
                _ScoreBoardShape.Paint += painter.OnPaint;
                _Container.Shapes.Add(_ScoreBoardShape);
            }
            y += height + 5;
            ButtonPanel buttonWidgetPainter;
            {
                _InputWidgetShape.Location = new Point(505, y);
                _InputWidgetShape.Size = new Size(150, height);
                _InputWidgetShape.BackStyle = BackStyle.Opaque;
                _InputWidgetShape.BackColor = Color.WhiteSmoke;
                _InputWidgetShape.CornerRadius = 5;

                buttonWidgetPainter = new ButtonPanel(_Game, _InputWidgetShape);
                _InputWidgetShape.Paint += buttonWidgetPainter.OnPaint;
                _Container.Shapes.Add(_InputWidgetShape);
                
                buttonWidgetPainter.Refresh();
            }
            MessagePanel messagePanel;
            {
                y += height + 5;
                _MessageShape.Location = new Point(505, y);
                _MessageShape.Size = new Size(150, height);
                _MessageShape.BackStyle = BackStyle.Opaque;
                _MessageShape.BackColor = Color.WhiteSmoke;
                _MessageShape.CornerRadius = 5;

                messagePanel = new MessagePanel(_Game, _MessageShape);
                _MessageShape.Paint += messagePanel.OnPaint;
                _Container.Shapes.Add(_MessageShape);
            }
            
            foreach(GamePlayer player in _Game.Players) 
            {
                y += height + 5;
                AddPlayerPanel(player, new Point(505, y));
            }

            _GameDisplayer = new GameDisplayerPT(_Game, _Container, 
                buttonWidgetPainter, messagePanel);
            
            foreach (SimpleShape shape in this._GameDisplayer.Shapes.Values)
            {
                GameLocation loc = new GameLocation(shape.Location);
                ElementDisplayer e = new ElementDisplayerPT(
                    _GameDisplayer, shape, loc, this._Container, _Game);
            }

            _Game.Start();
        }

        private void AddPlayerPanel(GamePlayer pGamePlayer, Point point)
        {
            RectangleShape shape = new RectangleShape();
            shape.Location = point;
            shape.Size = new Size(150, 90);
            shape.BackStyle = BackStyle.Opaque;
            shape.BackColor = Color.WhiteSmoke;
            shape.CornerRadius = 5;
    
            PlayerPanel painter = 
                new PlayerPanel(pGamePlayer, shape);
            shape.Paint += painter.OnPaint;
            _Container.Shapes.Add(shape);
            this._PlayerValueShapes.Add(shape);
        }
    
    }
    
    
}
